package com.lottery.bootdo.common.controller.system;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lottery.bootdo.common.service.ActivityInfoService;
import com.lottery.bootdo.common.utils.PageUtils;
import com.lottery.bootdo.common.utils.StringUtils;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.entity.*;
import com.lottery.common.service.DecryptToObjectService;
import com.lottery.common.service.TextInfoService;
import com.lottery.common.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

import static com.lottery.common.contants.Constants.*;

/**
 * @author o551wwl
 */
@Controller
@RequestMapping("/activity")
@Slf4j
@Api(value = "/activity",tags="富文本操作类")
public class ActivitysController {
	private ReQueryResult result = new ReQueryResult();

	@Autowired
	private ActivityInfoService activityService;
	@Autowired
	private TextInfoService textInfoService;
	@Autowired
	private DecryptToObjectService decryptToObjectService;

	@ApiOperation(value = "轮播图列表")
	@RequiresPermissions("activity:list")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	String index() {
		return "common/activity/index";
	}

	@ApiOperation(value = "轮播图详情")
	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	@RequiresPermissions("activity:detail")
	String detail(@PathVariable() Long id, Model model) {
		ActivityInfo info=activityService.getById(id);
		model.addAttribute("info", info);
		return "common/activity/detail";
	}

	@ApiOperation(value = "轮播图修改页")
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	@RequiresPermissions("activity:edit")
	String edit( @PathVariable() Long id, Model model) {
		ActivityInfo info=activityService.getById(id);
		if (info==null){
			info=new ActivityInfo();
		}
		model.addAttribute("info", info);
		return "common/activity/edit";
	}


	@ApiOperation(value = "投注推荐页")
	@RequiresPermissions("activity:bet:index")
	@RequestMapping(value = "/bet", method = RequestMethod.GET)
	String bet(Model model) {
		QueryWrapper<TextInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("type","0407");
		TextInfo textInfo=textInfoService.getOne(queryWrapper);
		model.addAttribute("info", textInfo);
		return "common/activity/bet/index";
	}
	@ApiOperation(value = "会员特权")
	@RequiresPermissions("activity:privilege:index")
	@RequestMapping(value = "/privilege", method = RequestMethod.GET)
	String privilege(Model model) {
		QueryWrapper<TextInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("type","0401");
		TextInfo textInfo=textInfoService.getOne(queryWrapper);
		model.addAttribute("info", textInfo);
		return "common/activity/privilege/index";
	}

	@ApiOperation(value = "平台分成页")
	@RequiresPermissions("activity:shared:index")
	@RequestMapping(value = "/shared", method = RequestMethod.GET)
	String shared(Model model) {
		SysConfig sysConfig= RedisUtils.getConfigFromRedis("0201",SysConfig.class);
		model.addAttribute("sysConfig", sysConfig);
		return "common/activity/shared";
	}

	@ApiOperation(value = "关于我们页")
	@RequiresPermissions("activity:text:index")
	@RequestMapping(value = "/text", method = RequestMethod.GET)
	String text(Model model) {
		QueryWrapper<TextInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.in("type","0402","0403","0404","0405","0406","0408","0409");
		List<TextInfo> list=textInfoService.list(queryWrapper);
		for(TextInfo textInfo:list){
			if (StringUtils.isNotBlank(textInfo.getText())){
				textInfo.setText(textInfo.getText().substring(0,textInfo.getText().length()<100?textInfo.getText().length():100));
			}
			textInfo.setStr(RedisUtils.getConfigFromRedis(textInfo.getType(),SysConfig.class).getValue());
		}
		model.addAttribute("list", list);
		return "common/activity/text/index";
	}

	@ApiOperation(value = "富文本编辑页")
	@RequiresPermissions("activity:text:edit")
	@RequestMapping(value = "/text/edit/{id}", method = RequestMethod.GET)
	String textEdit(@PathVariable() Long id, Model model) {
		TextInfo info=textInfoService.getById(id);
		SysConfig sysConfig=RedisUtils.getConfigFromRedis(info.getType(),SysConfig.class);
		model.addAttribute("info", info);
		model.addAttribute("value",sysConfig.getValue());
		return "common/activity/text/edit";
	}

	@ApiOperation(value = "vip定制index页")
	@RequiresPermissions("activity:custom:index")
	@RequestMapping(value = "/custom", method = RequestMethod.GET)
	String custom(Model model) {
		List<SysConfig> list= RedisUtils.getConfigFromRedisByType("00",SysConfig.class);
		model.addAttribute("list", list);
		return "common/activity/custom/index";
	}

	@ApiOperation(value = "会员价格页")
	@RequiresPermissions("activity:member:price")
	@RequestMapping(value = "/member", method = RequestMethod.GET)
	String memberPrice(Model model) {
		List<SysConfig> list= RedisUtils.getConfigFromRedisByType("03",SysConfig.class);
		for(SysConfig sysConfig:list){
			NutMap nutMap= JSON.parseObject(sysConfig.getValue(),NutMap.class);
			sysConfig.setValue(nutMap.getString("value"));
			sysConfig.setDiscount(nutMap.getString("discount"));
		}
		model.addAttribute("list", list);
		return "common/activity/member/index";
	}

	@ApiOperation(value = "更新投注推荐")
	@RequiresPermissions("activity:bet:edit")
	@RequestMapping(value = "/bet", method = RequestMethod.POST)
	@ResponseBody
	ReQueryResult updateBet(TextInfo textInfo) {
		log.info("__________ updateText");
		result = new ReQueryResult();
		textInfoService.updateById(textInfo);
		result.success(SUCCESS_DESC);
		return result;
	}

	@ApiOperation(value = "更新会员特权")
	@RequiresPermissions("activity:privilege:edit")
	@RequestMapping(value = "/privilege", method = RequestMethod.POST)
	@ResponseBody
	ReQueryResult updatePrivilege(TextInfo textInfo) {
		log.info("__________ updatePrivilege");
		result = new ReQueryResult();
		textInfoService.updateById(textInfo);
		result.success(SUCCESS_DESC);
		return result;
	}

	@ApiOperation(value = "更新平台分成")
	@RequiresPermissions("activity:bet:edit")
	@RequestMapping(value = "/shared", method = RequestMethod.POST)
	@ResponseBody
	ReQueryResult updateShared(String ratio) {
		log.info("__________ updateShared");
		result = new ReQueryResult();
		SysConfig sysConfig= RedisUtils.getConfigFromRedis("0201",SysConfig.class);
		sysConfig.setValue(ratio);
		result=RedisUtils.setConfigFromRedis(sysConfig);
		return result;
	}

	@ApiOperation(value = "更新富文本")
	@RequiresPermissions("activity:text:edit")
	@RequestMapping(value = "/text", method = RequestMethod.POST)
	@ResponseBody
	ReQueryResult updateText(TextInfo textInfo) {
		log.info("__________ updateText");
		result = new ReQueryResult();
		textInfoService.updateById(textInfo);
		result.success(SUCCESS_DESC);
		return result;
	}

	@ApiOperation(value = "更新vip定制")
	@RequiresPermissions("activity:custom:edit")
	@RequestMapping(value = "/custom", method = RequestMethod.POST)
	@ResponseBody
	ReQueryResult updateCustom(SysConfig config) {
		log.info("__________ updateText");
		result = new ReQueryResult();
		SysConfig sysConfig= RedisUtils.getConfigFromRedis(config.getCode(),SysConfig.class);
		sysConfig.setValue(config.getValue());
		result=RedisUtils.setConfigFromRedis(sysConfig);
		result.success(SUCCESS_DESC);
		return result;
	}
	@ApiOperation(value = "更新会员价格")
	@RequiresPermissions("activity:member:edit")
	@RequestMapping(value = "/member", method = RequestMethod.POST)
	@ResponseBody
	ReQueryResult updateMemberPrice(SysConfig config) {
		log.info("__________ updateText");
		result = new ReQueryResult();
		SysConfig sysConfig= RedisUtils.getConfigFromRedis(config.getCode(),SysConfig.class);
		NutMap nutMap=new NutMap();
		nutMap.setv("value",config.getValue());
		nutMap.setv("discount",config.getDiscount());
		sysConfig.setValue(JSON.toJSONString(nutMap));
		sysConfig.setRemark(config.getRemark());
		result=RedisUtils.setConfigFromRedis(sysConfig);
		result.success(SUCCESS_DESC);
		return result;
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	@RequiresPermissions("activity:list")
	@ResponseBody
	public PageUtils activityAll(String json){
		log.info("__________ activityAll");
		result = new ReQueryResult();
		NutMap nutMap=decryptToObjectService.jsonToObject(json,NutMap.class);
		log.info("nutmap is {}", JSON.toJSONString(nutMap));

		QueryWrapper<ActivityInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("is_del",1);
		Page<ActivityInfo> page = new Page<>(nutMap.getInt("pageIndex"),nutMap.getInt("pageLength"));

		IPage<ActivityInfo> records=activityService.page(page,queryWrapper);
		log.info(JSON.toJSONString(records));
		PageUtils pageUtils = new PageUtils(records.getRecords(),new BigDecimal(records.getTotal()).intValue());
		return pageUtils;
	}


	@RequestMapping(value = "", method = RequestMethod.POST)
	@RequiresPermissions("activity:edit")
	@ResponseBody
	ReQueryResult saveOrUpdate( ActivityInfo info) {
		log.info("__________ update");
		result = new ReQueryResult();
		if (info.getId()==null){
			info.setStatus(TYPE_ONE);
			info.setIsDel(TYPE_ONE);
			QueryWrapper<ActivityInfo> queryWrapper=new QueryWrapper<>();
			queryWrapper.eq("is_del",TYPE_ONE);
			Integer count=activityService.count(queryWrapper);
			if (count>=5){
				result.error("最多添加五张");
				return result;
			}
		}
		if (info.getIsIo()==2){
			info.setDetailShowType(TYPE_TWO);
		}
		activityService.saveOrUpdate(info);
		result.success(SUCCESS_DESC);
		return result;
	}

	@RequestMapping(value = "", method = RequestMethod.DELETE)
	@RequiresPermissions("activity:del")
	@ResponseBody
	ReQueryResult activityDel( ActivityInfo info) {
		log.info("__________ update");
		result = new ReQueryResult();
		//已添加的小于等于 1个的时候禁止删除
		if (info.getIsDel()==TYPE_ZERO){
			QueryWrapper<ActivityInfo> queryWrapper=new QueryWrapper<>();
			queryWrapper.eq("is_del",TYPE_ONE);
			Integer count=activityService.count(queryWrapper);
			if (count<=1){
				result.error("不能全部删除");
				return result;
			}
		}
		activityService.updateById(info);
		result.success(SUCCESS_DESC);
		return result;
	}

	@RequestMapping(value = "", method = RequestMethod.PATCH)
	@RequiresPermissions("activity:status")
	@ResponseBody
	ReQueryResult activityStatus( ActivityInfo info) {
		log.info("__________ update");
		result = new ReQueryResult();
		//已上线的小于等于 1个的时候禁止删除

		if (info.getStatus()==TYPE_TWO){
			QueryWrapper<ActivityInfo> queryWrapper=new QueryWrapper<>();
			queryWrapper.eq("is_del",TYPE_ONE);
			queryWrapper.eq("status",TYPE_ONE);
			Integer count=activityService.count(queryWrapper);
			if (count<=1){
				result.error("不能全部禁用");
				return result;
			}
		}
		activityService.updateById(info);
		result.success(SUCCESS_DESC);
		return result;
	}

}
